// Author:
//   Jason Couture, plaguethenet@gmail.com
//
// Copyright (c) 2007-2011, Jason Couture
//
// All rights reserved.
//
// If you wish to use XML Settings in your application, YOU MUST FIRST request permission.
// This source is provided to the llAma project to ease the build process on linux systems.
// It may be modified and used with llAma only, And not in another project, or released on
// its own, Without my express written permssion. AKA, ALL RIGHTS RESERVED.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections.Generic;
using System.Text;

namespace XMLSettings
{
    public class SettingsCache
    {
        private static Dictionary<string, Setting> ReadCache = new Dictionary<string, Setting>();
        private static Dictionary<string, Setting> WriteCache = new Dictionary<string, Setting>();
        private static DateTime LastWrite = System.DateTime.Now;
        private static object objLock = new object();
        public static bool Commit(bool force)
        {
            return ProcessCache(force);
        }
        public static Setting ReadSetting(string Filename)
        {
            string Key = Filename.ToLower().Trim();
            if (ReadCache.ContainsKey(Key))
            {
                return ReadCache[Key];
            }
            else
            {
                return null;
            }
        }
        public static void WriteSetting(string Filename, Setting S)
        {
            lock (objLock)
            {
                string Key = Filename.ToLower().Trim();
                if (S == null)
                {
                    WriteCache.Remove(Key);
                    ReadCache.Remove(Key);
                }
                if (WriteCache.ContainsKey(Key))
                {
                    WriteCache[Key] = S;
                }
                else
                {
                    WriteCache.Add(Key, S);
                }
                if (ReadCache.ContainsKey(Key))
                {
                    ReadCache[Key] = S;
                }
                else
                {
                    ReadCache.Add(Key, S);
                }
            }
        }
        private static int delaytime = 300;
        public static int CacheWriteDelay
        {
            get { return delaytime; }
            set { delaytime = value; }
        }
        private static void commit()
        {
            lock (objLock)
            {
                foreach (KeyValuePair<string, Setting> D in WriteCache)
                {
                    D.Value.Save();
                }
                WriteCache.Clear();
            }
        }
        private static bool ProcessCache(bool force)
        {
            TimeSpan tsLastWrite = LastWrite - DateTime.Now;
            if (tsLastWrite.TotalSeconds > delaytime)
            {
                LastWrite = DateTime.Now;
                commit();
                return true;
            }
            else
            {
                if (force == true)
                {
                    LastWrite = DateTime.Now;
                    commit();
                    return true;
                }
            }
            return false;
        }
        ~SettingsCache()
        {
            // We need to commit the cache if it indeed exists.
            ProcessCache(true);
        }
    }
}
